﻿Partial Class pukl_Auto
    Inherits System.Web.UI.Page
    Public shopingCount As String
    Private dt As System.Data.DataTable
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        lblMsg.Text = String.Empty
        Title = "以車型產品方式查詢..."
        Utility.LoadJquery(Me, Me.GetType())
        Utility.LoadAutoComplete(Me, Me.GetType)


        If (Not IsPostBack) Then
            showShopButton()
        End If
        System.Web.UI.ScriptManager.RegisterClientScriptBlock( _
            Me, Me.GetType(), "autoCar", ucCARac.GetAutoCompleteScript(), True)

        System.Web.UI.ScriptManager.RegisterClientScriptBlock( _
            Me, Me.GetType(), "autProd", ucPRODUCTac.GetAutoCompleteScript(), True)


        If (Not Session("shopCar") Is Nothing) Then

            Dim sc As shopCar
            sc = CType(Session("shopCar"), shopCar)
            shopingCount = String.Format("目前已選擇了{0}樣商品" _
                        , sc.GetShopCount().ToString())
        End If
    End Sub

    Sub btnExecute_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If (Not ucCARac.IsHaveValue() And Not ucPRODUCTac.IsHaveValue()) Then
            ShowMessage("請輸入查詢條件！")
            GV1.DataSource = Nothing
            GV1.DataBind()

            showShopButton()
            Exit Sub
        End If

        Session("car") = ucCARac.GetName()
        Session("prod") = ucPRODUCTac.GetName()

        getData()
    End Sub
    Sub getData()
        Dim car, prod As String
        car = Session("car")
        prod = Session("prod")
        dt = Utility.GetCarProdData(car, prod)
        If (dt.Rows.Count.Equals(0)) Then
            Session("car") = Nothing
            Session("prod") = Nothing
        End If
        GV1.DataSource = dt
        GV1.DataBind()
        showShopButton()
    End Sub

    Protected Sub showShopButton()
        Dim script As String
        script = String.Empty

        If (GV1.Rows.Count.Equals(0)) Then

            script = " $(function()" + _
                "{" + _
                "    $('.shoping').hide()" + _
                "});"
        Else
            script = " $(function()" + _
                    "{" + _
                    "    $('.shoping').show();" + _
                    "});"
        End If
        System.Web.UI.ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "SHOpPING", script, True)
    End Sub
    Protected Sub btnShoping1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnShoping1.Click, btnShoping.Click
        shoping()
    End Sub

    Protected Sub shoping()
        '--還要有一個顯示東西的網頁
        Dim chk As CheckBox
        Dim lbl As Label
        'Dim shopCount As New ArrayList

        Dim sc As shopCar
        If (Session("shopCar") Is Nothing) Then
            sc = New shopCar
        Else
            sc = CType(Session("shopCar"), shopCar)
        End If

        For Each row In GV1.Rows
            chk = CType(row.Cells(0).FindControl("chkShoping"), CheckBox)
            If Not (chk Is Nothing) Then
                If (chk.Checked) Then
                    lbl = CType(row.Cells(0).FindControl("lblPu_id"), Label)
                    sc.Add(lbl.Text)
                End If
            End If
        Next

        Session("shopCar") = sc
        shopingCount = String.Format("目前已選擇了{0}樣商品" _
                        , sc.GetShopCount().ToString())
    End Sub

    Protected Sub GV1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GV1.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            Dim lbl As Label
            lbl = CType(e.Row.Cells(0).FindControl("lblPu_id"), Label)
            lbl.Text = dt.Rows(e.Row.RowIndex)("PUID").ToString()
            'lbl.Text = 
        End If
    End Sub

    Protected Sub ShowMessage(ByVal p_msg As String)
        Dim script As String
        script = String.Format("alert('{0}');", p_msg)

        ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "empty", script, True)


    End Sub

    Protected Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click

    End Sub
End Class
